//
//  UIImage+Mango.m
//  Mango
//
//  Created by 胡超 on 2018/1/29.
//  Copyright © 2018年 胡超., ltd. All rights reserved.
//

#import "UIImage+Mango.h"
#import "UIColor+Mango.h"

@implementation UIImage (Mango)

+ (instancetype)image:(MGColor)color {
    return [self image:color size:CGSizeMake(1, 1) cornerRadius:0.0];
}

+ (instancetype)image:(MGColor)color size:(CGSize)size {
    return [self image:color size:size cornerRadius:0.0];
}

+ (instancetype)image:(MGColor)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius {
    UIColor *uiColor = [UIImage mg_getColor:color];
    if (!uiColor) {
        uiColor = UIColor.blackColor;
    }
    
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, UIScreen.mainScreen.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius] addClip];
    CGContextSetFillColorWithColor(context, uiColor.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (instancetype)gradientImage:(NSArray<MGColor> *)colors {
    return [self gradientImage:colors locations:nil start:CGPointZero end:CGPointMake(0, 1.0) size:CGSizeMake(10.0, 10.0)];
}

+ (instancetype)gradientImage:(NSArray<MGColor> *)colors size:(CGSize)size {
    return [self gradientImage:colors locations:nil start:CGPointZero end:CGPointMake(0, 1.0) size:size];
}

+ (instancetype)gradientImage:(NSArray<MGColor> *)colors start:(CGPoint)start end:(CGPoint)end {
    return [self gradientImage:colors locations:nil start:start end:end size:CGSizeMake(10.0, 10.0)];
}

+ (instancetype)gradientImage:(NSArray<MGColor> *)colors start:(CGPoint)start end:(CGPoint)end size:(CGSize)size {
    return [self gradientImage:colors locations:nil start:start end:end size:size];
}

+ (instancetype)gradientImage:(NSArray<MGColor> *)colors locations:(NSArray<NSNumber *>*)locations start:(CGPoint)start end:(CGPoint)end size:(CGSize)size {
    NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:colors.count];
    for (id color in colors) {
        UIColor *uiColor = [self mg_getColor:color];
        if (uiColor) {
            [cgColors addObject:(__bridge id)uiColor.CGColor];
        }
    }
    if (cgColors.count == 0) {
        return nil;
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    gradientLayer.startPoint = start;
    gradientLayer.endPoint = end;
    gradientLayer.colors = cgColors;
    gradientLayer.locations = locations;
    
    UIGraphicsBeginImageContextWithOptions(gradientLayer.frame.size, gradientLayer.opaque, UIScreen.mainScreen.scale);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return gradientImage;
}

- (UIImage *)originalRenderingImage {
    return [self imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

- (UIImage *)clipCornerRadius:(CGFloat)cornerRadius {
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, UIScreen.mainScreen.scale);
    [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius] addClip];
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)scaleTo:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (UIImage *)fill:(MGColor)color  {
    UIColor *uiColor = [UIImage mg_getColor:color];
    if (!uiColor) {
        return self;
    }
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextClipToMask(context, rect, self.CGImage);
    [uiColor setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

#pragma mark - Private

+ (UIColor *)mg_getColor:(MGColor)color {
    if ([color isKindOfClass:UIColor.class]) {
        return color;
    } else if ([color isKindOfClass:NSString.class]) {
        return UIColor.Hex(color);
    } else {
        return nil;
    }
}

@end
